'''
=========================
!/usr/bin/env python
_*_ coding:utf-8 _*_

@Time : 2020/7/13 9:35
@Coder: xh
@IDE  : PyCharm
=========================
'''
import requests
import base64



def get_access_token():
    # client_id 为官网获取的AK， client_secret 为官网获取的SK
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=cCvSUD2W8EmDiqPW2Gc2fuCj&client_secret=355HvFchLWgjB7nCdDnGoRSmbIoM8Gbq'
    response = requests.get(host)
    if response:
        access_token = response.json()['access_token']
    return access_token


def get_animation():
    '''
    人像动漫化
    '''
    request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
    # 二进制方式打开图片文件
    f = open('mm.jpg', 'rb')
    img = base64.b64encode(f.read())

    params = {"image": img}
    access_token = get_access_token()
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        animation_img = response.json()
    return animation_img

def decode_img(img):
    f = open('无口罩mm.jpg','wb')
    decoded_img = img['image']
    decoded_img = base64.b64decode(decoded_img)
    f.write(decoded_img)
    f.close()

if __name__ == '__main__':
    img = get_animation()
    decode_img(img)